package StudyCode.summary.myfile;

import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DomainNameUsingDNSEncoding {
	public static void main(String[] argv) {
		String domainName = "tah1986.com";
		System.out.println(Arrays.toString(encodeDomainName(domainName)));
	}

	private static byte[] encodeDomainName(String domainName) {

		byte[] result = new byte[domainName.length() + 2];
		int position = 0;
		StringTokenizer tokenizer = new StringTokenizer(domainName, ".@");

		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken();
			result[position] = (byte) token.length();
			position++;
			for (byte b : token.getBytes()) {
				result[position] = b;
				position++;
			}
		}

		result[position] = (byte) 0;
		return result;
	}
}
